<template>
  <div class="access">
    <section class="table-form">
      <div class="table-toolbar">
        <div class="btns">
          <el-button icon="el-icon-plus" size="mini" type="primary" plain>新增</el-button>
          <!-- <el-button icon="el-icon-edit" size="mini" type="success" plain
            >修改</el-button
          >
          <el-button icon="el-icon-delete" size="mini" type="danger" plain
            >删除</el-button
          >
          <el-button icon="el-icon-search" size="mini" type="warning" plain
            >导出</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" type="primary" plain
            >刷新</el-button
          > -->
        </div>
        <div class="right">
          <el-tooltip content="隐藏搜索栏" placement="top">
            <el-button circle size="mini" icon="el-icon-search"></el-button>
          </el-tooltip>
          <el-tooltip content="刷新数据" placement="top">
            <el-button circle size="mini" icon="el-icon-refresh"></el-button>
          </el-tooltip>
        </div>
      </div>
      <el-table
        stripe
        fit
        highlight-current-row
        style="width: 100%"
        :indent="60"
        :data="state.tableData"
      >
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          align="center"
          v-for="item in state.tableFields"
          :class="item.prop"
          :key="item.prop"
          v-bind="item"
        >
          <template #default="{ row }" v-if="item.prop == 'Address'">
            <el-tag>{{
              `
              ${row.Address.ad_info.nation}
              ${row.Address.ad_info.province}
              ${row.Address.ad_info.city}
              ${row.Address.ad_info.district}
              `
            }}</el-tag>
          </template>
          <template #default="{ row }" v-if="item.prop == 'UA.browser'">
            <el-tag>{{ row.UA.browser.name }}</el-tag>
          </template>
          <template #default="{ row }" v-if="item.prop == 'UA.os'">
            <el-tag>{{ row.UA.os.name }} {{ row.UA.os.version }}</el-tag>
          </template>
          <template #default="{ row }" v-if="item.prop == 'createdAt'">
            {{ dayjs(row.createdAt).format('YYYY-MM-DD - HH:MM:ss') }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template #default="scope">
            <div class="btns">
              <el-button circle size="mini" icon="el-icon-view"></el-button>
              <el-button circle size="mini" icon="el-icon-edit"></el-button>
              <el-popconfirm title="你确定要删除此条记录么？" @confirm="() => scope.row._id">
                <template #reference>
                  <el-button circle size="mini" type="danger" icon="el-icon-delete"></el-button>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- <el-pagination
        @size-change="sizeChange"
        class="pagination"
        :current-page="1"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="20"
        layout="total, sizes, prev, pager, next, jumper"
        :total="1"
      ></el-pagination> -->
    </section>
  </div>
</template>

<script lang="ts" setup>
import dayjs from 'dayjs'
import { onMounted, reactive, ref } from 'vue'
import { getAccessList } from '../api/access'

const init = async () => {
  const access = await getAccessList()
  state.tableData = access
}

onMounted(() => {
  init()
})

const state = reactive<any>({
  tableData: [],
  tableFields: [
    {
      label: '访问编号',
      prop: '_id',
    },
    // {
    //   label: '登录名称',
    //   prop: 'name',
    // width: '100px',
    // },
    {
      label: '主机',
      prop: 'Address.ip',
    },
    {
      label: '登录地点',
      prop: 'Address',
    },
    {
      label: '浏览器',
      prop: 'UA.browser',
    },
    {
      label: '操作系统',
      prop: 'UA.os',
    },
    {
      label: '访问时间',
      prop: 'createdAt',
    },
  ],
})
</script>

<style lang="scss" scoped>
.access {
  .search-bar {
    background-color: #fff;
    box-sizing: border-box;
    width: 100%;
    padding: 10px;
    .el-form {
      .inputs {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        .input {
          flex-basis: 240px;
          justify-content: flex-start;
          margin: 10px;
        }
        .btns {
          text-align: center;
          // justify-content: flex-start;
        }
        .el-form-item {
          text-align: center;
          // margin-bottom: 0;
        }
      }
    }
  }
  .table-form {
    margin-top: 20px;
    .table-toolbar {
      @include flex-center;
      padding: 10px;
      background-color: #fff;
      justify-content: flex-start;
      .right {
        margin-left: auto;
      }
    }
    .btns {
      // display: flex;
      // flex-wrap: wrap;
      // justify-content: flex-start;
      .el-button {
        margin: 0px;
        margin: 5px;
      }
    }
    .pagination {
      margin-top: 20px;
      text-align: right;
    }
  }
}
</style>
